package org.dizena.modules.wss.dao;

import org.dizena.base.avo.req.JqGridReq;
import org.dizena.base.dao.BaseDao;
import org.dizena.base.dao.DbDao;
import org.dizena.modules.wss.bean.Message;
import org.dizena.utils.ObjectUtil;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;


@Repository
public class MessageDao extends BaseDao implements DbDao<Message>
{
    @Resource
    private MongoTemplate dao;

    @Override
    public Message add(Message m)
    {
        return dao.save(m);
    }

    @Override
    public void deleteById(String id)
    {
        dao.remove(idQuery(id), Message.class);
    }

    @Override
    public void deleteBy(String[] keys, Object[] vals)
    {
        Query query = keyIsVal(keys, vals);
        dao.remove(query, Message.class);
    }

    @Override
    public void update(Message m)
    {
        try
        {
            Update update = new Update();

            Map<String, Object> map = ObjectUtil.formObjectToMap(m);
            for (Map.Entry<String, Object> e : map.entrySet())
            {
                update.set(e.getKey(), e.getValue());
            }
            dao.updateFirst(idQuery(m.getId()), update, Message.class);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public Message get(String id)
    {
        return dao.findOne(idQuery(id), Message.class);
    }

    @Override
    public List<Message> findMany(String[] keys, Object[] vals)
    {
        Query query = keyIsVal(keys, vals);
        return dao.find(query, Message.class);
    }

    @Override
    public Long count(String[] keys, Object[] vals)
    {
        Query query = keyIsVal(keys, vals);
        return dao.count(query, Message.class);
    }

    @Override
    public List<Message> find(Long page, Integer size, String[] keys, Object[] vals)
    {
        Query query = keyIsVal(keys, vals);
        if (page != null && size != null)
        {
            query.skip((page - 1) * size);
            query.limit(size);
        }
        return dao.find(query, Message.class);
    }

    @Override
    public List<Message> queryAll(JqGridReq req, Map<String, Object> map)
    {

        return dao.find(allQuery(req, map), Message.class);
    }

    @Override
    public Long count(String searchField, Map<String, Object> map)
    {
        Query query = map2QueryLike(map, searchField);
        return dao.count(query, Message.class);
    }

    public List<Message> getNotSendToReceiver(String receiver)
    {
        String[] keys = {"receiver", "imStatus"};
        Object[] values = {receiver, 1};
        Query query = keyIsVal(keys, values);
        query.with(Sort.by(Sort.Direction.ASC, "createTime"));
        return dao.find(query, Message.class);
    }

    public List<Message> getChat(String fromUid, String toUid)
    {
        String[] keys = {"sender", "receiver"};
        String[] values = {fromUid, toUid};
        Query query = keyIsVal(keys, values);
        query.limit(50);
        query.with(Sort.by(Sort.Direction.DESC, "createTime"));
        List<Message> list1 = dao.find(query, Message.class);

        String[] vales2 = {toUid, fromUid};
        Query query2 = keyIsVal(keys, vales2);
        query2.limit(50);
        query2.with(Sort.by(Sort.Direction.DESC, "createTime"));
        List<Message> list2 = dao.find(query2, Message.class);

        List<Message> all = new ArrayList<Message>();
        all.addAll(list1);
        all.addAll(list2);
        Collections.sort(all);
        return all;
    }


}
